package com.neusoft.neuiotms.dcom.service.insurance.impl;

import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.converter.insurance.InsuranceRegisterConverter;
import com.neusoft.neuiotms.dcom.dto.insurance.InsuranceRegisterDTO;
import com.neusoft.neuiotms.dcom.model.insurance.InsuranceRegister;
import com.neusoft.neuiotms.dcom.repository.insurance.InsuranceRegisterRepository;
import com.neusoft.neuiotms.dcom.service.insurance.InsuranceRegisterService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class InsuranceRegisterServiceImpl implements InsuranceRegisterService {

    @Autowired
    private InsuranceRegisterRepository registerRepository;
    @Autowired
    private InsuranceRegisterConverter registerConverter;

    @Override
    public List<InsuranceRegisterDTO> index(final Searchable searchable, final UserBean user) {
        final List<InsuranceRegister> models = this.registerRepository.index(searchable);
        return this.registerConverter.toListDTO(models);
    }

    @Override
    public Page<InsuranceRegisterDTO> page(final Searchable searchable, final Pageable pageable, final UserBean user) {
        final Page<InsuranceRegister> models = this.registerRepository.search(pageable, searchable);
        return this.registerConverter.toPageDTO(models);
    }

    @Transactional
    @Override
    public InsuranceRegisterDTO create(final InsuranceRegisterDTO dto, final UserBean user) {
        final InsuranceRegister model = this.registerConverter.toModel(dto);
        this.registerRepository.save(model);
        return this.registerConverter.toDTO(model);
    }

    @Override
    public InsuranceRegisterDTO update(final Long id, final InsuranceRegisterDTO dto, final UserBean user) {
        InsuranceRegister model;
        try {
            model = this.registerRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            InsuranceRegisterServiceImpl.log.error(e.getMessage(), e);
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        // 已生成任务不允许进行修改时间与发布状态
        if (model.getRegisterTask().size() > 0) {
            if ((dto.getReleaseStatus() != model.getReleaseStatus())
                    || (dto.getStartTime().getTime() != model.getStartTime().getTime())
                    || (dto.getEndTime().getTime() != model.getEndTime().getTime())) {
                throw new UnitedException("已生成任务，不允许修改时间与发布状态");
            }
        }

        //保存
        dto.setId(model.getId());
        model = this.registerConverter.toModel(dto);
        this.registerRepository.save(model);

        return this.registerConverter.toDTO(model);

    }

    @Override
    public InsuranceRegisterDTO detail(final Long id) {
        try {
            final InsuranceRegister model = this.registerRepository.getOne(id);
            return this.registerConverter.toDTO(model);
        } catch (final EntityNotFoundException e) {
            InsuranceRegisterServiceImpl.log.error(e.getMessage(), e);
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

    }

    @Override
    public void delete(final Long id) {
        try {
            this.registerRepository.deleteById(id);
        } catch (final EntityNotFoundException e) {
            InsuranceRegisterServiceImpl.log.error(e.getMessage(), e);
            throw new UnitedException(e.getMessage(), new Object[] {id });
        }
    }

    @Override
    public InsuranceRegisterDTO updateStatus(final Long id, final Boolean status) {

        InsuranceRegister model;
        try {
            model = this.registerRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            InsuranceRegisterServiceImpl.log.error(e.getMessage(), e);
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        final InsuranceRegisterDTO dto = this.registerConverter.toDTO(model);
        dto.setReleaseStatus(status);
        model = this.registerConverter.toModel(dto);
        this.registerRepository.save(model);

        return this.registerConverter.toDTO(model);
    }

}
